using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace cb0tServer
{
    partial class ServerCore
    {
        private void OnLinkedAdminCommand(LinkedUserObject lUser, String command_ident, UserObject tUser, String args)
        {
            if (tUser == null || lUser == null)
                return; // all linked commands have a target

            switch (command_ident)
            {
                case "ban":
                    if (tUser.level > 0) return; // linked admins can never ban a local admin
                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(89).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnouncePacket(StringTemplate.GetString(89).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.banstats.Add("linked admin: " + tUser.name + " [" + tUser.physical_ip.ToString() + "] was banned by linked admin: " + lUser.name);
                    Bans.AddBan(tUser);
                    tUser.Disconnect();
                    this.OnPart(tUser);
                    Stats.UsersParted++;
                    Stats.BannedUsers++;
                    Stats.KickedUsers++;
                    AdminLog.WriteLine("linked admin: " + lUser.name + " banned " + tUser.name);
                    break;

                case "kill":
                    if (tUser.level > 0) return; // linked admins can never kill a local admin
                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(88).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnouncePacket(StringTemplate.GetString(88).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    tUser.Disconnect();
                    this.OnPart(tUser);
                    Stats.UsersParted++;
                    Stats.KickedUsers++;
                    AdminLog.WriteLine("linked admin: " + lUser.name + " kicked " + tUser.name);
                    break;

                case "muzzle":
                    if (tUser.level > 0) return; // linked admins can never muzzle a local admin
                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(91).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnouncePacket(StringTemplate.GetString(91).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    tUser.muzzled = true;
                    Stats.MuzzledUsers++;
                    Muzzles.AddMuzzle(tUser);
                    AdminLog.WriteLine("linked admin: " + lUser.name + " muzzled " + tUser.name);
                    break;

                case "unmuzzle":
                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(92).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnouncePacket(StringTemplate.GetString(92).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    tUser.muzzled = false;
                    Muzzles.RemMuzzle(tUser);
                    AdminLog.WriteLine("linked admin: " + lUser.name + " unmuzzled " + tUser.name);
                    break;

                case "clone":
                    switch (args.Split(new string[] { " " }, StringSplitOptions.None)[0])
                    {
                        case "/me":
                            if (args.Length > 4)
                            {
                                UserPool.Broadcast(0, ServerOutboundPackets.EmoteTextPacket(tUser.name, args.Substring(4)));
                                this.link_client.SendOutboundPacket(ServerOutboundPackets.EmoteTextPacket(tUser.name, args.Substring(4)));
                            }

                            break;

                        default:
                            if (tUser.customname)
                            {
                                UserPool.Broadcast(0, ServerOutboundPackets.AnnoucePacket(tUser.customname_text + args));
                                this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnouncePacket(tUser.customname_text + args));
                            }
                            else
                            {
                                UserPool.Broadcast(0, ServerOutboundPackets.PublicTextPacket(tUser.name, args));
                                this.link_client.SendOutboundPacket(ServerOutboundPackets.PublicTextPacket(tUser.name, args));
                            }

                            break;
                    }

                    UserPool.BroadcastToAdmins(ServerOutboundPackets.AnnoucePacket("\x000314" + lUser.name + " cloned " + tUser.name));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAdminMessage("\x000314" + lUser.name + " cloned " + tUser.name));
                    AdminLog.WriteLine("linked admin: " + lUser.name + " cloned " + tUser.name);
                    break;

                case "echo":
                    if (args.Length < 1) return;
                    tUser.echo = true;
                    tUser.echo_text = args;
                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(84).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(84).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    AdminLog.WriteLine("linked admin: " + lUser.name + " echoed " + tUser.name);
                    break;

                case "unecho":
                    tUser.echo = false;
                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(85).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(85).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    AdminLog.WriteLine("linked admin: " + lUser.name + " unechoed " + tUser.name);
                    break;

                case "kiddy":
                    tUser.kiddied = true;
                    Kiddys.AddKiddy(tUser);
                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(82).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(82).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    AdminLog.WriteLine("linked admin: " + lUser.name + " kiddied " + tUser.name);
                    break;

                case "unkiddy":
                    tUser.kiddied = false;
                    Kiddys.RemKiddy(tUser);
                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(83).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(83).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    AdminLog.WriteLine("linked admin: " + lUser.name + " unkiddied " + tUser.name);
                    break;

                case "paint":
                    if (args.Length < 1) return;
                    tUser.painted = true;
                    tUser.painted_text = args;
                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(86).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(86).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    AdminLog.WriteLine("linked admin: " + lUser.name + " painted " + tUser.name);
                    break;

                case "unpaint":
                    tUser.painted = false;
                    UserPool.Broadcast(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(87).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    this.link_client.SendOutboundPacket(ServerOutboundPackets.AnnoucePacket(StringTemplate.GetString(87).Replace("+a", Settings.stealth ? CoreSettings.room_bot : lUser.name).Replace("+n", tUser.name)));
                    AdminLog.WriteLine("linked admin: " + lUser.name + " unpainted " + tUser.name);
                    break;

                case "changename":
                    String t = tUser.name;

                    if (this.OnNameChanging(tUser, args))
                    {
                        UserPool.BroadcastToAdmins(ServerOutboundPackets.AnnoucePacket("14" + lUser.name + " has changed " + t + "'s name to " + tUser.name));
                        this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAdminMessage("14" + lUser.name + " has changed " + t + "'s name to " + tUser.name));
                        AdminLog.WriteLine("linked admin: " + lUser.name + " changed " + t + "'s name to " + tUser.name);
                    }
                    else
                    {
                        this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnounceToUser(lUser.name, "14Name already in use!"));
                    }

                    break;

                case "oldname":

                    if (this.OnNameChanging(tUser, tUser.orgName))
                    {
                        UserPool.BroadcastToAdmins(ServerOutboundPackets.AnnoucePacket("14" + lUser.name + " has restored " + tUser.name + "'s name"));
                        this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAdminMessage("14" + lUser.name + " has restored " + tUser.name + "'s name"));
                        AdminLog.WriteLine(lUser.name + " restored " + tUser.name + "'s name");
                    }

                    break;
            }
        }

        private String LinkFilterText(LinkedUserObject lUser, String text)
        {
            if (lUser.admin) return text;

            String str = Helpers.StripColors(text);
            FilterItem[] items = WordFilters.GetFilters();

            if (items == null) return text;

            foreach (FilterItem filter in items)
            {
                if (str.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                {
                    switch (filter.type)
                    {
                        case "replace":
                            if (Regex.IsMatch(text, filter.trigger, RegexOptions.IgnoreCase))
                                text = Regex.Replace(text, filter.trigger, filter.args, RegexOptions.IgnoreCase);
                            break;

                        case "muzzle":
                            this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnounceToUser(lUser.name,
                                StringTemplate.GetString(36).Replace("+n", lUser.name)));
                            return "";

                        case "kiddy":
                            this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnounceToUser(lUser.name,
                                StringTemplate.GetString(36).Replace("+n", lUser.name)));
                            return "";

                        case "kill":
                            this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnounceToUser(lUser.name,
                                StringTemplate.GetString(36).Replace("+n", lUser.name)));
                            return "";

                        case "ban":
                            this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnounceToUser(lUser.name,
                                StringTemplate.GetString(36).Replace("+n", lUser.name)));
                            return "";

                        case "censor":
                            this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnounceToUser(lUser.name,
                                StringTemplate.GetString(36).Replace("+n", lUser.name)));
                            return "";

                        case "move":
                            this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnounceToUser(lUser.name,
                                StringTemplate.GetString(36).Replace("+n", lUser.name)));
                            return "";

                        case "redirect":
                            this.link_client.SendOutboundPacket(ServerOutboundPackets.LinkAnnounceToUser(lUser.name,
                                StringTemplate.GetString(36).Replace("+n", lUser.name)));
                            return "";

                        default:
                            break;
                    }
                }
            }

            return text;
        }

        private bool LinkFilterPM(LinkedUserObject lUser, String text)
        {
            if (lUser.admin) return true;

            String str = Helpers.StripColors(text);
            FilterItem[] items = PMFilters.GetFilters();

            if (items == null) return true;

            foreach (FilterItem filter in items)
                if (str.ToUpper().IndexOf(filter.trigger.ToUpper()) > -1)
                    return false;

            return true;
        }
    }
}
